package diamond.day03;

public class ThreadTest1 {
    public static void main(String[] args) {
        Account account = new Account();
        Thread personA = new Thread(account);
        Thread personB = new Thread(account);
        personA.start();
        personB.start();
    }
}

/**
 * 账号类
 */
class Account implements Runnable{
    private double balance;  // 账户余额

    /**
     * 存钱
     * @param money 存的钱数
     */
    private synchronized void saveMoney(double money){
        if(money>0){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.balance = this.balance + money;
            System.out.println(Thread.currentThread().getName()+"存钱成功，账户余额为："+this.balance);
        }
    }
    public void run() {
        // 分三次存入
        for(int i = 0;i<3;i++){
            saveMoney(1000);
        }
    }
}
